// TOWN SCRIPT
//    Town 23: Realms of Mist

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

short i,j,k,r1,choice;
string dlgstr;
body;

beginstate INIT_STATE;
		set_state_continue(80);
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
// This state is called every turn the party is in this town.
break;

beginstate 10;
	reset_dialog();
	add_dialog_str(0,"A portal flickers strangely before you.",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Enter.");
	choice = run_dialog(1);
	if (choice == 2)
		set_state_continue(12);
	set_state_continue(11);
break;

beginstate 11;
	block_entry(1);
break;

beginstate 12;
	teleport_party(34,8,0);
break;

beginstate 13;
	if (is_combat()) {
	 end();
	}
	set_state_continue(14);
break;

beginstate 14;
	if (get_flag(23,0) >= 2)
		set_state_continue(10);
	set_state_continue(15);
break;

beginstate 15;
	message_dialog("An unseen force throws you back.","");
	block_entry(1);
break;

beginstate 16;
	reset_dialog();
	add_dialog_str(0,"You hear an echoing voice through the mist...",0);
	add_dialog_str(1,"_What is it that can speak every language in the world?_",0);
	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(17);
break;

beginstate 17;
	get_text_response("You Respond:");
		check_text_response_match("echo");
		 if (got_text_match())
			set_state_continue(19);
	set_state_continue(26);
break;

beginstate 18;
	if (get_flag(23,1) == 250)
		end();
	set_state_continue(16);
break;

beginstate 19;
	inc_flag(23,0,1);
	set_state_continue(20);
break;

beginstate 20;
	if (get_flag(23,1) == 250)
		end();
	message_dialog("There is a chime.","");
	set_flag(23,1,250);
break;

beginstate 21;
	if (get_flag(23,2) == 250)
		end();
	set_state_continue(22);
break;

beginstate 22;
	reset_dialog();
	add_dialog_str(0,"Faint letters appear as though from a distance...",0);
	add_dialog_str(1,"_Water on sand not under water._",0);
	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(23);
break;

beginstate 23;
	get_text_response("You Respond:");
		check_text_response_match("mirage");
		 if (got_text_match())
			set_state_continue(24);
	set_state_continue(26);
break;

beginstate 24;
	inc_flag(23,0,1);
	set_state_continue(25);
break;

beginstate 25;
	if (get_flag(23,2) == 250)
		end();
	message_dialog("There is a chime.","");
	set_flag(23,2,250);
break;

beginstate 26;
	message_dialog("Nothing happens.","");
	set_state_continue(81);
break;

beginstate 27;
	if (get_flag(23,3) == 250)
		end();
	set_state_continue(28);
break;

beginstate 28;
	reset_dialog();
	add_dialog_str(0,"A cold voice cries from somewhere beyond...",0);
	add_dialog_str(1,"_Glittering points that downward thrust, sparkling spears that never rust._",0);
	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(29);
break;

beginstate 29;
	get_text_response("You Respond:");
		check_text_response_match("icicle");
		 if (got_text_match())
			set_state_continue(30);
		check_text_response_match("icicles");
		 if (got_text_match())
			set_state_continue(30);			
	set_state_continue(26);
break;

beginstate 30;
	inc_flag(23,0,1);
	set_state_continue(31);
break;

beginstate 31;
	if (get_flag(23,3) == 250)
		end();
	message_dialog("There is a chime.","");
	set_flag(23,3,250);
break;

beginstate 32;
	if (get_flag(23,4) == 250)
		end();
	set_state_continue(33);
break;

beginstate 33;
	reset_dialog();
	add_dialog_str(0,"A dark, hollow voice speaks in your mind...",0);
	add_dialog_str(1,"_He who makes it needs it not; he who buys it wants it not; he who uses it feels it not._",0);
	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(34);
break;

beginstate 34;
	get_text_response("You Respond:");
		check_text_response_match("coffin");
		 if (got_text_match())
			set_state_continue(35);
		check_text_response_match("grave");
		 if (got_text_match())
			set_state_continue(35);
	set_state_continue(26);
break;

beginstate 35;
	inc_flag(23,0,1);
	set_state_continue(36);
break;

beginstate 36;
	if (get_flag(23,4) == 250)
		end();
	message_dialog("There is a chime.","");
	set_flag(23,4,250);
break;

beginstate 37;
	if (is_combat()) {
	block_entry(1);
	 end();
	}
	set_state_continue(38);
break;

beginstate 38;
	if (get_flag(23,0) >= 4)
		set_state_continue(39);
	set_state_continue(15);
break;

beginstate 39;
	reset_dialog();
	add_dialog_str(0,"A portal flickers strangely before you.",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Enter.");
	choice = run_dialog(1);
	if (choice == 2)
		set_state_continue(40);
	set_state_continue(11);
break;

beginstate 40;
	teleport_party(43,55,0);
break;

beginstate 41;
	if (get_flag(23,5) == 250)
		end();
	set_state_continue(42);
break;

beginstate 42;
	reset_dialog();
	add_dialog_str(0,"A plain voice speaks from before you...",0);
	add_dialog_str(1,"_It runs through hills, veers around mountains, leaps over rivers, and crawls through forests, yet is never elusive to those who know where they go._",0);
	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(43);
break;

beginstate 43;
	get_text_response("You Respond:");
		check_text_response_match("road");
		 if (got_text_match())
			set_state_continue(44);
		check_text_response_match("path");
		 if (got_text_match())
			set_state_continue(44);
	set_state_continue(26);
break;

beginstate 44;
	inc_flag(23,0,1);
	set_state_continue(45);
break;

beginstate 45;
	if (get_flag(23,5) == 250)
		end();
	message_dialog("There is a chime.","");
	set_flag(23,5,250);
break;

beginstate 46;
	if (get_flag(23,6) == 250)
		end();
	set_state_continue(47);
break;

beginstate 47;
	reset_dialog();
	add_dialog_str(0,"A booming voice rolls through the mist...",0);
	add_dialog_str(1,"_Traveling always in circles, yet always straight ahead._",0);
	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(48);
break;

beginstate 48;
	get_text_response("You Respond:");
		check_text_response_match("wheel");
		 if (got_text_match())
			set_state_continue(49);
	set_state_continue(26);
break;

beginstate 49;
	inc_flag(23,0,1);
	set_state_continue(50);
break;

beginstate 50;
	if (get_flag(23,6) == 250)
		end();
	message_dialog("There is a chime.","");
	set_flag(23,6,250);
break;

beginstate 51;
	if (is_combat()) {
	block_entry(1);
	end();
	}
	set_state_continue(52);
break;

beginstate 52;
	if (get_flag(23,0) >= 6)
		set_state_continue(53);
	set_state_continue(15);
break;

beginstate 53;
	reset_dialog();
	add_dialog_str(0,"A portal flickers strangely before you.",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Enter.");
	choice = run_dialog(1);
	if (choice == 2)
		set_state_continue(54);
	set_state_continue(11);
break;

beginstate 54;
	teleport_party(41,31,0);
break;

beginstate 55;
	set_state_continue(56);
break;

beginstate 56;
	reset_dialog();
	add_dialog_str(0,"The mists swirl around you. Beyond the icy river to the west, a gargantuan figure of living fire looms to the dome of the cavern. Beasts of the Zephyr prowl at its feet, and you suddenly find that the path to the south is blocked as well.",0);
	add_dialog_str(1,"The howling of the wind has utterly ceased. No noise is there about you, no familiar whisper of voices. The fog separates and reforms in the shape of a sculpture.",0);
	add_dialog_str(2,"It seems as though the statue of a lizard-man, or at least was. There is nothing left of it but rubble: nothing except a half-buried head and a long, cracked tail. The fog-sculpture hangs in the air.",0);
	clear_buffer();
		append_string("Hint, think of the meaning of: ");
		append_number(coins_amount());
		append_string(".");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);

	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(57);
break;

beginstate 57;
	get_text_response("You Respond:");
		check_text_response_match("coins");
		 if (got_text_match())
			set_state_continue(59);
	set_state_continue(58);
break;

beginstate 58;
	if (get_flag(23,7) == 250)
		end();
	set_flag(23,7,250);
	message_dialog("You hope, for a brief while, that you'd be able to make off with a wrong answer. For a few moments, the response is halted in the air as your answer echoes through the cavern.",
	  "Then the beasts pounce.");
	activate_hidden_group(1);
break;

beginstate 59;
	reset_dialog();
	add_dialog_str(0,"The sculpture disintegrates into a fine mist. Evidently, you have given the correct answer. Your relief is cut short, however, when you see that the Zephyr beasts are still in their places. Their master shows no sign of relentment.",0);
	add_dialog_str(1,"The fog regathers, forming letters that you understand...",0);
	add_dialog_str(2,"--- NAME IT, AND SO BREAK IT. ---",0);
	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(60);
break;

beginstate 60;
	get_text_response("You Respond:");
		check_text_response_match("silence");
		 if (got_text_match())
			set_state_continue(61);
	set_state_continue(58);
break;

beginstate 61;
	reset_dialog();
	add_dialog_str(0,"Your answer cuts through the fog as a spear, shattering the letters of the riddle. A clamorous din begins to rise beside you. Shrill cries of alien voices, and the baying of a thousand hounds, and breaking and fusing, all in uneven tone and rhythm...",0);
	add_dialog_str(1,"Thoughts dart through your mind at a frenzied pace. One image occurs and reoccurs, burning it into your memory...",0);
	add_dialog_str(2,"_Twistable, malleable might it be, but ever straight as an arrow._",0);
	add_dialog_choice(0,"Attempt to Answer.");
	choice = run_dialog(1);
	set_state_continue(62);
break;

beginstate 62;
	get_text_response("You Respond:");
		check_text_response_match("truth");
		 if (got_text_match())
			set_state_continue(63);
	set_state_continue(58);
break;

beginstate 63;
	message_dialog("The forms around you fade into nothing.","");
	set_flag(23,0,7);
	set_state_continue(68);
break;

beginstate 64;
	if (is_combat()) {
	block_entry(1);
	end();
	}
	set_state_continue(65);
break;

beginstate 65;
	if (get_flag(23,0) >= 7)
		set_state_continue(66);
	set_state_continue(83);
break;

beginstate 66;
	reset_dialog();
	add_dialog_str(0,"A portal flickers strangely before you.",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Enter.");
	choice = run_dialog(1);
	if (choice == 2)
		set_state_continue(67);
	set_state_continue(11);
break;

beginstate 67;
	teleport_party(54,8,0);
break;

beginstate 68;
	set_flag(23,7,1);
break;

beginstate 69;
	message_dialog("An unseen force throws you back. Apparently, it's easier getting in than getting out.","");
	block_entry(1);
break;

beginstate 70;
	message_dialog("An unseen force throws you back.","");
	block_entry(1);
break;

beginstate 71;
	i = 6;
	while (i < NUM_CHARS) {
		if ((char_ok(i)) && (get_attitude(i) >= 10))
			erase_char(i);
		i = i + 1;
		}
	set_state_continue(72);
break;

beginstate 72;
	set_flag(23,0,7);
break;

beginstate 73;
	if (get_flag(23,8) == 250)
		end();
	set_flag(23,8,250);
	message_dialog("A small sphere levitates in mid-air, black, round, and perfectly smooth. It circles about an unseen axis, floating invitingly. Prompted by a sudden impulse, you stretch out your hand to take it.",
	  "Before you can reach it yourself, though, it drifts into your grasp. You get a vague sense of satisfaction.");
	change_spec_item(4,1);
	set_terrain(56,6,0);
break;

beginstate 74;
	if (is_combat()) {
	block_entry(1);
	end();
	}
	set_state_continue(75);
break;

beginstate 75;
	if (has_special_item(4) > 0)
		set_state_continue(77);
	set_state_continue(76);
break;

beginstate 76;
	message_dialog("It's probably a poor idea to leave without getting anything. You turn back.","");
	block_entry(1);
break;

beginstate 77;
	reset_dialog();
	add_dialog_str(0,"A portal flickers strangely before you.",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Enter.");
	choice = run_dialog(1);
	if (choice == 2)
		set_state_continue(78);
	set_state_continue(11);
break;

beginstate 78;
	block_entry(1);
		move_to_new_town(15,16,18);
		end();
break;

beginstate 79;
	reset_dialog();
	add_dialog_str(0,"The world before you is wrapped in an unbreakable mist. Ever swirling before you, the mist prevents you from seeing more than a few feet in front.",0);
	add_dialog_str(1,"A cold wind whispers quietly, but borne upon it you hear a multitude of voices, yet all so faint that there is nothing you can discern.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
break;

beginstate 80;
	if (get_flag(94,0) == 250)
		end();
	reset_dialog();
	add_dialog_str(0,"The world before you shifts and turns, engulfed in a strange dust or fog...",0);
	add_dialog_str(1,"Phantoms and shadows dance before you, along with things of more substance and more danger than merely to the mind. Or perhaps it is all in the mind.",0);
	add_dialog_str(2,"There are beasts fanged and clawed, there are wraiths of a hungry darkness. There are, too, glimmers of ephemeral light, some pleasant and welcoming, some dim and dizzying and sick.",0);
	add_dialog_str(3,"The mist swirls. A strange light reaches you through it, but you cannot be sure of the source. There is only the impenetrable cloud, and the uncertainties that lie beyond...",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_flag(94,0,250);
break;

beginstate 81;
	if (character_in_party(1702) >= 0) 
		set_state_continue(82);
break;

beginstate 82;
	if (get_flag(94,8) == 250)
		end();
	message_dialog("Fernow cocks her head in thought. _That's not it. I think it has to do with the way the question was voiced. Listen again..._","");
	set_flag(94,8,250);
break;

beginstate 83;
	if (get_flag(139,2) >= 1)
		set_state_continue(66);
	set_state_continue(15);
break;

